#include "setwig.h"
#include "ui_setwig.h"


#define TOPIC_DATA_UP       "/app/data/up"
#define TOPIC_DATA_DOWN     "/app/data/down"
#define TOPIC_CTL_UP        "/app/control/up"
#define TOPIC_CTL_DOWN      "/app/control/down"

SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig)
{
    ui->setupUi(this);

}

SetWig::~SetWig()
{
    delete ui;

}


//散热按钮
void SetWig::on_sanrebutton_clicked()
{
    sanre_value = false;
    if(sanre_value == false)
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"sanre\":true}");
        qDebug() << "[SET]:" << "{\"sanre\":true}";
        GlobalVal::mqtt->publish(msg);
        sanre_value = true;
    }
    else
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"sanre\":false}");
        qDebug() << "[SET]:" << "{\"sanre\":false}";
        GlobalVal::mqtt->publish(msg);
        sanre_value = false;
    }
}

//接触按钮
void SetWig::on_jiechubutton_clicked()
{
    jiechu_value = false;
    if(jiechu_value == false)
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"jiechu\":true}");
        qDebug() << "[SET]:" << "{\"jiechu\":true}";
        GlobalVal::mqtt->publish(msg);
        jiechu_value = true;
    }
    else
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"jiechu\":false}");
        qDebug() << "[SET]:" << "{\"jiechu\":false}";
        GlobalVal::mqtt->publish(msg);
        jiechu_value = false;
    }
}

//警报按钮
void SetWig::on_jingbaobutton_clicked()
{
    jingbao_value = false;
    if(jingbao_value == false)
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"jingbao\":true}");
        qDebug() << "[SET]:" << "{\"jingbao\":true}";
        GlobalVal::mqtt->publish(msg);
        jingbao_value = true;
    }
    else
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"jingbao\":false}");
        qDebug() << "[SET]:" << "{\"jingbao\":false}";
        GlobalVal::mqtt->publish(msg);
        jingbao_value = false;
    }
}

//湿度按钮
void SetWig::on_shidubutton_clicked()
{
    shidu_value = false;
    if(shidu_value == false)
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"shidu\":true}");
        qDebug() << "[SET]:" << "{\"shidu\":true}";
        GlobalVal::mqtt->publish(msg);
        shidu_value = true;
    }
    else
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"shidu\":false}");
        qDebug() << "[SET]:" << "{\"shidu\":false}";
        GlobalVal::mqtt->publish(msg);
        shidu_value = false;
    }

}

//温度显示
void SetWig::wendu(const QByteArray &msg)
{
    qDebug() << "[SET]:" << msg;

    QJsonDocument json = QJsonDocument::fromJson(msg);
    if(json.isNull())
    {
        qDebug() << "[SET]:" << "JSON格式错误";
        return;
    }
    if(json.isObject())
    {
        QJsonObject root = json.object();

        QList<QString> keys = root.keys();
        for(int i = 0;i<keys.size();i++)
        {
            QString key = keys.at(i);
            QJsonValue value = root.value(key);
            qDebug() << "[SET]:" << key << ":" << value;
        }
        // 利用[]重载，根据键值获取数值
        double temp_value = root["temp"].toDouble();
        qDebug() << "\"temp\":" << temp_value;
        ui->wendulcd->display(temp_value);
    }
}

//湿度显示
void SetWig::shidu(const QByteArray &msg)
{
    qDebug() << "[SET]:" << msg;

    QJsonDocument doc = QJsonDocument::fromJson(msg);
    if(doc.isNull())
    {
        qDebug() << "[SET]:" << "JSON格式错误";
        return;
    }
    if(doc.isObject())
    {
        QJsonObject root = doc.object();

        QList<QString> keys = root.keys();
        for(int i = 0;i<keys.size();i++)
        {
            QString key = keys.at(i);
            QJsonValue value = root.value(key);
            qDebug() << "[SET]:" << key << ":" << value;
        }
        // 利用[]重载，根据键值获取数值
        double hum_value = root["hum"].toDouble();
        qDebug() << "\"hum\":" << hum_value;
        ui->shidulcd->display(hum_value);
    }
}



void SetWig::on_LEDcheck_1_clicked()
{
    if(ui->LEDcheck_1->isChecked())
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"LED1\":true}");
        qDebug() << "[SET]:" << "{\"LED1\":true}";
        GlobalVal::mqtt->publish(msg);
    }
    else
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"LED1\":false}");
        qDebug() << "[SET]:" << "{\"LED1\":false}";
        GlobalVal::mqtt->publish(msg);
    }
}

void SetWig::on_LEDcheck_2_clicked()
{
    if(ui->LEDcheck_2->isChecked())
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"LED2\":true}");
        qDebug() << "[SET]:" << "{\"LED2\":true}";
        GlobalVal::mqtt->publish(msg);
    }
    else
    {
        QMQTT::Message msg;
        msg.setTopic(TOPIC_CTL_DOWN);
        msg.setPayload("{\"LED2\":false}");
        qDebug() << "[SET]:" << "{\"LED2\":false}";
        GlobalVal::mqtt->publish(msg);
    }
}
